package com.ncloud.system.mapper;

import com.ncloud.system.entity.Depart;
import com.ncloud.system.entity.DepartRole;
import com.ncloud.system.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by shiqiang on 2017/7/28.
 */
@Mapper
public interface DepartMapper {
    @Select(value = { "select a.*,b.company_name from sys_depart a,sys_company b where a.company_id = b.company_id and a.use_tag='1'  and a.depart_id=#{0}" })
    @Results({ @Result(column = "depart_id", property = "departId"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "company_name", property = "companyName"),
            @Result(column = "depart_name", property = "departName"), @Result(column = "use_tag", property = "useTag"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    public Depart getOneDepart(Long departId);
    /*根据公司查询其下的分组*/
    @Select(value = { "select a.*,b.company_name from sys_depart a,sys_company b where a.company_id = b.company_id and a.use_tag='1'  and b.company_id=#{0}" })
    @Results({ @Result(column = "depart_id", property = "departId"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "company_name", property = "companyName"),
            @Result(column = "depart_name", property = "departName"), @Result(column = "use_tag", property = "useTag"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    List<Depart> getDepartsByCompanyId(Long companyId);

    /*查询系统下所有分组*/
    @Select(value = { "select a.*,b.company_name from sys_depart a,sys_company b where a.company_id = b.company_id and a.use_tag='1' " })
    @Results({ @Result(column = "depart_id", property = "departId"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "company_name", property = "companyName"),
            @Result(column = "depart_name", property = "departName"), @Result(column = "use_tag", property = "useTag"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    List<Depart> getDepartsAll();
    @Insert("insert into sys_depart(depart_id,company_id, depart_name,use_tag,create_user, create_date, update_date, update_user, remarks)"
            + " values(#{departId},#{companyId}, #{departName}, #{useTag}, #{createUser}, #{createDate}, #{updateDate}, #{updateUser}, #{remarks})")
    public int insertDepart(Depart depart);

    @Update("update sys_depart set company_id=#{companyId}, depart_name=#{departName}, use_tag=#{useTag}, "
            + " update_date=#{updateDate}, update_user=#{updateUser} where depart_id=#{departId} and use_tag='1'")
    public int updateDepart(Depart depart);
    /*删除部门*/
    @Delete("delete from sys_depart  where depart_id=#{0} and use_tag='1'")
    public int deleteDepart(Long departId);
    /*修改账户组状态*/
    @Update("update sys_depart set use_tag='0' where depart_id=#{0} and use_tag='1'")
    public int deleteDepartState(Long departId);
    /*插入部门角色关系*/
    @Insert("insert into sys_depart_role(depart_id,role_id, use_tag)"
            + " values(#{departId},#{roleId}, #{useTag})")
    public int insertDepartRole(DepartRole departRole);

    /*删除部门角色*/
    @Delete("delete from sys_depart_role where depart_id=#{0}")
    int deleteDepartRole(Long departId);

    @Update("update sys_depart_role set use_tag='0' where depart_id=#{0} and use_tag='1'")
    public int deleteDepartRoletate(Long departId);

    @Select(value = { "select DISTINCT b.* from sys_depart_role a,sys_role b where a.role_id= b.role_id " +
            " and a.depart_id=#{0} and a.use_tag ='1'" })
    @Results({ @Result(column = "role_id", property = "roleId"), @Result(column = "company_id", property = "companyId"),
            @Result(column = "role_name", property = "roleName"), @Result(column = "use_tag", property = "useTag"),
            @Result(column = "create_user", property = "createUser"),
            @Result(column = "create_date", property = "createDate"),
            @Result(column = "update_date", property = "updateDate"),
            @Result(column = "update_user", property = "updateUser"),
            @Result(column = "remarks", property = "remarks") })
    List<Role> getRolesByDepartId(Long departId);


    /*获取组下的账户数*/
    @Select("select count(1) count from sys_depart a ,sys_depart_role b ,sys_user_role c,sys_user d "
            +" where a.DEPART_ID=b.DEPART_ID and d.depart_id=a.DEPART_ID and b.ROLE_ID=c.role_id and c.user_id=d.user_id " +
            " and a.use_tag ='1' and a.DEPART_ID =#{0}")
    int getDepartAccountNum(Long departId);

}
